<?php
namespace app\admin\controller\resources;

use app\admin\controller\Controller;
use app\admin\model\resources\Course as CourseModel;
class Course extends Controller
{
    /**
     * 课件列表
     * @param string $nickName 昵称
     * @param int $gender 性别
     * @param int $grade 会员等级
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new CourseModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));

    }


    /**
     * 课件添加
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $model = new CourseModel;
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        // 新增记录
        if ($model->add($this->postData('course'))) {
            return $this->renderSuccess('添加成功', url('resources.course/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');

    }

    /**
     * 更新课件
     * @param $course_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($course_id)
    {
        // 文章详情
        $model = CourseModel::detail($course_id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 更新记录
        if ($model->edit($this->postData('course'))) {
            return $this->renderSuccess('更新成功', url('resources.course/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除课件
     * @param $course_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($course_id)
    {
        // 文章详情
        $model = CourseModel::detail($course_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }


}